home *** CD-ROM | disk | FTP | other *** search
- using System;
- using System.ComponentModel;
- using System.IO;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Navigation;
- using System.Collections.Generic;
- using System.Windows.Ink;
- using Microsoft.Win32;
-
- namespace UntitledProject1
- {
- public partial class Window1
- {
- public static readonly RoutedCommand ExitCommand = new RoutedCommand("Exit", typeof(Window1));
-
- private FlipBook flipBook = null;
-
- // Current pencil selected.
- private FrameworkElement currentPencil;
-
- // Keep the original position of the selected pencil to be able
- // return it back in place when another one is selected.
- private Point keepDockPosPencil;
-
- // Uses mouse position to update current pencil selection.
- private double mouseX, mouseY;
-
- #region Initialization
-
- public Window1()
- {
- this.DataContext = this;
- this.InitializeComponent();
- this.CommandBindings.Add(new CommandBinding(ApplicationCommands.New, this.NewFlipBook));
- this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Open, this.OpenFlipBook));
- this.CommandBindings.Add(new CommandBinding(ApplicationCommands.SaveAs, this.SaveFlipBookAs));
- this.CommandBindings.Add(new CommandBinding(Window1.ExitCommand, this.Exit));
- }
-
- private void NewFlipBook(object sender, EventArgs e)
- {
- this.flipBook.Clear();
- }
-
- /// <summary>
- /// After all the elements are ready, bind the CLR Object Data Source
- /// This is an alternative to: protected override void OnInitialized(EventArgs e)
- /// since when OnLoaded fires all the elements are initialized.
- /// </summary>
- private void OnLoaded(object sender, RoutedEventArgs e)
- {
- ObjectDataProvider odp = FindResource("FlipBookDS") as ObjectDataProvider;
- if (odp != null)
- {
- flipBook = odp.Data as FlipBook;
- }
- currentPencil = colorBlack;
- ActivatePencil(currentPencil);
- }
-
- #endregion
-
- #region File
-
- private void OpenFlipBook(object caller, EventArgs e)
- {
- OpenFileDialog fileDialog = new OpenFileDialog();
- fileDialog.Title = "Open Animation";
- fileDialog.RestoreDirectory = true;
- fileDialog.InitialDirectory = "../../Animations";
- fileDialog.Filter = "Flipbook Animations (*.fba)|*.fba";
- bool? dialogResult = fileDialog.ShowDialog(this);
-
- if (dialogResult.HasValue && dialogResult.Value)
- {
- try
- {
- this.flipBook.Load(fileDialog.FileName);
- }
- catch (Exception exception)
- {
- string error = String.Format("Cannot open file '{0}'.\n{1}", fileDialog.FileName, exception.Message);
- MessageBox.Show(this, error, "Cannot open file", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- }
-
- private void SaveFlipBookAs(object caller, EventArgs e)
- {
- SaveFileDialog fileDialog = new SaveFileDialog();
- fileDialog.Title = "Save Animation As";
- fileDialog.RestoreDirectory = true;
- fileDialog.InitialDirectory = "../../Animations";
- fileDialog.Filter = "Flipbook Animations (*.fba)|*.fba";
- bool? dialogResult = fileDialog.ShowDialog(this);
-
- if (dialogResult.HasValue && dialogResult.Value)
- {
- try
- {
- this.flipBook.Save(fileDialog.FileName);
- }
- catch (Exception exception)
- {
- string error = String.Format("Cannot save file '{0}'.\n{1}", fileDialog.FileName, exception.Message);
- MessageBox.Show(this, error, "Cannot save file", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- }
-
- private void Exit(object sender, EventArgs e)
- {
- Application.Current.Shutdown();
- }
-
- #endregion
-
- #region Pencil
-
- /// <summary>
- /// When the user clicks one of the pencils, change the selection.
- /// </summary>
- private void OnGrabPencil(object sender, RoutedEventArgs e)
- {
- Canvas.SetZIndex(currentPencil, 0);
- Canvas.SetLeft(currentPencil, keepDockPosPencil.X);
- Canvas.SetTop(currentPencil, keepDockPosPencil.Y);
- currentPencil.Width = 55;
- currentPencil.Visibility = Visibility.Visible;
- this.MouseMove -= new MouseEventHandler(DragPencil);
- currentPencil = sender as FrameworkElement;
- ActivatePencil((FrameworkElement)sender);
- }
-
- /// <summary>
- /// This method updates the selection with the new mouse.
- /// </summary>
- void ActivatePencil(FrameworkElement pencil)
- {
- Button currentSelected = pencil as Button;
- if (flipBook != null) flipBook.InkColor = (Color)ColorConverter.ConvertFromString(currentSelected.Background.ToString());
- Canvas.SetZIndex(currentPencil, 10);
- currentPencil.Width = 177;
- keepDockPosPencil = new Point(Canvas.GetLeft(currentPencil), Canvas.GetTop(currentPencil));
- UpdatePencilPosition();
- this.MouseMove += new MouseEventHandler(DragPencil);
- }
-
- void DragPencil(object sender, MouseEventArgs e)
- {
- UpdatePencilPosition();
- }
-
- private void UpdatePencilPosition()
- {
- UpdateMousePos();
- Canvas.SetLeft(currentPencil, mouseX - 29);
- Canvas.SetTop(currentPencil, mouseY + 25);
- }
-
- private void UpdateMousePos()
- {
- Point mousePos = Mouse.GetPosition(studio);
- mouseX = mousePos.X;
- mouseY = mousePos.Y;
- }
-
- private void ShowPencil()
- {
- currentPencil.Visibility = Visibility.Visible;
- }
- private void HidePencil()
- {
- currentPencil.Visibility = Visibility.Hidden;
- }
-
- private void OnShowPencil(object sender, MouseEventArgs e)
- {
- ShowPencil();
- }
-
- private void OnHidePencil(object sender, MouseEventArgs e)
- {
- HidePencil();
- }
-
- #endregion
-
- #region Stroke Event
-
- /// <summary>
- /// When a new stroke size is selected it triggers this event.
- /// </summary>
- private void OnChangeStroke(object sender, RoutedEventArgs e)
- {
- RadioButton selected = sender as RadioButton;
- if (flipBook != null) flipBook.InkWidth = selected.FontSize;
- }
-
- #endregion
-
- #region RepeatMode Event
-
- /// <summary>
- /// Sets the current repeatMode, based on the menu item selected.
- /// Once - Plays the animations one time.
- /// Loop - Play in a infinite loop.
- /// Bounce - Plays and reverses the animation in an infinite loop.
- /// </summary>
- private void OnRepeatMode(object sender, RoutedEventArgs e)
- {
- if (flipBook != null)
- {
- MenuItem el = sender as MenuItem;
- RepeatMode mode = new RepeatMode();
- switch (el.Header.ToString())
- {
- case "_Once":
- mode = RepeatMode.Once;
- itemMenuLoop.IsChecked = false;
- itemMenuBounce.IsChecked = false;
- break;
- case "_Loop":
- mode = RepeatMode.Loop;
- itemMenuOnce.IsChecked = false;
- itemMenuBounce.IsChecked = false;
- break;
- case "_Bounce":
- mode = RepeatMode.Bounce;
- itemMenuOnce.IsChecked = false;
- itemMenuLoop.IsChecked = false;
- break;
- }
- flipBook.RepeatMode = mode;
- }
- }
-
- #endregion
-
- }
- }